package IOperations;

import method.*;
import node.Node;

import java.util.Scanner;

public class Multiplication {
    public static void work() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入被乘数整数：");
        String num1 = scanner.nextLine();
        if(!isDataSafe.work(num1)) {
            return;
        }
        System.out.print("请输入乘数整数：");
        String num2 = scanner.nextLine();
        if(!isDataSafe.work(num2)) {
            return;
        }

        Node head1 = createNum.work(num1);
        Node head2 = createNum.work(num2);

        //统一链表长度
        int lenHead1 = ListLength.work(head1);
        int lenHead2 = ListLength.work(head2);
        Node lonHead = lenHead1>=lenHead2?head1:head2;
        Node shortHead = lenHead1<lenHead2?head1:head2;
        int maxLen=lenHead1>lenHead2?lenHead1:lenHead2;
        int minLen=lenHead1<lenHead2?lenHead1:lenHead2;

        //开始乘数运算
        //1.二维表创建
        int row=minLen+1;
        int col=maxLen+1;
        Node curLon=lonHead.next;
        Node curShort=shortHead.next;
        int[][] form = new int[row][col];
        for(int i=0;i<maxLen;i++) {
            form[0][i]= curLon.val;
            curLon=curLon.next;
        }
        for(int i=1;i<row;i++) {
            form[i][col-1]=curShort.val;
            curShort=curShort.next;
        }
        //各项相乘
        for(int i=0;i<minLen;i++) {
            for(int n=0;n<col-1;n++) {
                form[i+1][n]=form[0][n]*form[i+1][col-1];
            }
        }
        //二维表相加操作
        int[] temp = new int[(row-1)+(col-1)-1];
        int dex=0;
        //横向
        for(int i=0;i<col-1;i++) {
            int sum=0;
            int curRow=1;
            int curCol=i;
            while(curRow<row&&curCol>=0) {
                sum+=form[curRow][curCol];
                curRow++;
                curCol--;
            }
            temp[dex++]=sum;
        }
        //纵向
        for(int i=2;i<row;i++) {
            int sum=0;
            int curRow=i;
            int curCol=col-2;
            while(curRow<row&&curCol>=0) {
                sum+=form[curRow][curCol];
                curRow++;
                curCol--;
            }
            temp[dex++]=sum;
        }
        //顺序表相加操作
        for(int i=(row-1)+(col-1)-2;i>0;i--) {
            if(temp[i]>9999) {
                temp[i-1]+=temp[i]/10000;
                temp[i]%=10000;
            }
        }
        //打印
        String ret = new String();
        for(int i=0;i<(row-1)+(col-1)-1;i++) {
            ret=ret.concat(String.format("%04d",temp[i]));
        }
        //对多余的0处理
        int i=0;
        while(ret.charAt(i)=='0') {
            if(i==ret.length()-1) {
                break;
            }
            i++;
        }
        ret=ret.substring(i,ret.length());
        //进行正负号处理
        if((lonHead.val==1&&shortHead.val==0)||(lonHead.val==0&&shortHead.val==1)) {
            ret="-".concat(ret);
        }
        System.out.println(ret);
    }
}
